<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends CommonController {
	//分类列表
    public function index()
    {

        //显示category
        $cate = M('category')->select();
        $this->assign('cate',$cate);

        $this->display();
    }

    //edit 显示
    public function edit()
    {
        $id = I('get.id');
        $sql = 'select * from category where id='.$id;
        $categoryinfo = M()->query($sql);
        $this->assign('categoryinfo',current($categoryinfo));

        //栏目选择显示
        $sql = 'select * from category';
        $arr = M()->query($sql);
        $cate = $this->gettree($arr);
        $this->assign('cate',$cate);

        $this->display();
    }

    //edit
    public function edit_row()
    {
        //print_r($_POST);
        //[cname] => ccc[fid] => 1[categoryid] => 6
        if (IS_POST) {
            $id = I('post.categoryid');
            $data['cname'] = I('post.cname');
            $data['fid'] = I('post.fid');

            $res = M('category')->where(array('id'=>$id))->save($data);
            if ($res) {
                $arr = array(
                "error" => 1,
                "info" => "栏目修改成功"
                );
            echo json_encode($arr);
            }else {
                $arr = array(
                    "error" => 0,
                    "info" => "栏目修改失败"
                    );
                echo json_encode($arr);
            }
        }


    }

    //创建栏目
    public function add()
    {
        //print_r($_POST);exit;
        if (IS_POST) {
            $data['cname'] = I('post.cname');
            $data['fid'] = I('post.fid');

            if (empty($data['cname'])) {
                $arr = array(
                    "error" => 0,
                    "info" => "栏目不能为空"
                    );
                echo json_encode($arr);
                exit;
            }

            $res = M('category')->add($data);
            if ($res) {
                $arr = array(
                "error" => 1,
                "info" => "栏目创建成功"
                );
            echo json_encode($arr);
            }else {
                $arr = array(
                    "error" => 0,
                    "info" => "栏目创建失败"
                    );
                echo json_encode($arr);
            }

        }


    }
}